#include <iostream>
#include <memory>

// 模板基类
template <typename T>
class Base {
public:
    virtual ~Base() = default;

    // 虚函数
    virtual void doSomething(const T& value) = 0;

    // 添加虚函数 updateSol
    //virtual void updateSol(const T& B, T& X) = 0;
};
// 派生类A
template <typename T>
class Derived : public Base<T> {
public:
    void doSomething(const T& value) {
        std::cout << "Derived class handling value: " << value << std::endl;
    }

    
    // 实现 updateSol 函数
    void updateSol(const T& B, T& X) {
        std::cout << "Updating solution with B: " << B << " and X: " << X << std::endl;
        // 假设这里有一些更新 X 的逻辑
        X = B + 1; // 示例逻辑
    }
};


// 派生类B
template <typename T>
class DerivedB : public Base<T> {
public:
    void doSomething(const T& value) override {
        std::cout << "Derived class handling value: " << value << std::endl;
    }

    // 重载函数
    void doSomething(const T& value, int extra) {
        std::cout << "Derived class handling value: " << value << " with extra: " << extra << std::endl;
    }

    // 实现 updateSol 函数
    void updateSol(const T& B, T& X) {
        std::cout << "Updating solution with B: " << B << " and X: " << X << std::endl;
        // 假设这里有一些更新 X 的逻辑
        X = B + 1; // 示例逻辑
    }
};

// 工厂类
template <typename T>
class Factory {
public:
    static std::unique_ptr<Base<T>> createDerived() {
        return std::make_unique<Derived<T>>();
    }
    static std::unique_ptr<Base<T>> createDerivedB() {
        return std::make_unique<DerivedB<T>>();
    }
};

int main() {
    // 使用工厂类创建Derived类的实例
    std::unique_ptr<Base<int>> basePtr = Factory<int>::createDerived();
    // 如果需要调用重载函数，需要通过Derived类的指针
    Derived<int>* derivedPtr = dynamic_cast<Derived<int>*>(basePtr.get());
    //basePtr->doSomething(42);
    std::unique_ptr<Base<int>> basePtrB = Factory<int>::createDerivedB();
    DerivedB<int>* derivedPtrB = dynamic_cast<DerivedB<int>*>(basePtrB.get());
    
    //basePtrB->doSomething(42, 100);
    if (derivedPtrB) {
        derivedPtrB->doSomething(42, 100);
    }
    // 调用 updateSol 函数
    int B = 5;
    int X = 0;
    derivedPtrB->updateSol(B, X);
    //basePtrB->updateSol(B, X);
    std::cout << "Updated X: " << X << std::endl;

    return 0;
}

